<?php

/**
 * uniBoard Help Page
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-06 01:45:14 +0000 (Mon, 06 Sep 2010) $
 * $Revision: 59 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_help{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;

		// Load Lang Files
		$appCore -> loadLanguageFile( 'help');
		
		// Load Templates
		$templates = new tpl_help_page( $appCore);
		
		// Page Vars
		$tpl_vars = array(
			'help_files_list' 	=> '',
			'help_files' 		=> ''
		);
				
		// Load help files from cache
		$help_files = class_cache::loadCache( 'help_files');
		
		// Build cache?
		if ( class_cache::$load_error)
		{
			// Select help files
			$appCore -> db -> query( 'SELECT * FROM ' . DB_PREFIX . 'help_files ORDER BY file_pos ASC');
			
			// Move them to array
			while ( $file_result = $appCore -> db -> fetch_assoc())
			{
				$help_files[$file_result['file_parent']][] = $file_result;
			}
			
			// Store cache
			class_cache::saveCache( 'help_files', $help_files);
		}
		
		// Build files?
		if ( isset( $help_files[0]))
		{
			// Items in help file
			$root_n = count( $help_files[0]);
			
			// Loop main files
			for ( $i = 0; $i < $root_n; $i ++)
			{
				// Do we have children items?
				if ( isset( $help_files[$help_files[0][$i]['file_id']]))
				{
					// Displayed Items List in Contents
					$category_items = '';
					
					// Displayed Children in Category
					$displayed_children = '';
					
					// Childrens number
					$children_n = count( $help_files[$help_files[0][$i]['file_id']]);
					
					// Loop them
					for ( $c = 0; $c < $children_n; $c ++)
					{
						// Put item in contents list
						$category_items .= $templates -> tpl_file_item( array(
							'item_id'		=> $help_files[$help_files[0][$i]['file_id']][$c]['file_id'],
							'item_name'		=> nl2br( $appCore -> ifString( htmlspecialchars( $help_files[$help_files[0][$i]['file_id']][$c]['file_name'])))
						));
						
						// Draw Children
						$displayed_children .= $templates -> tpl_file_body_item( array(
							'item_id'		=> $help_files[$help_files[0][$i]['file_id']][$c]['file_id'],
							'item_name'		=> nl2br( $appCore -> ifString( htmlspecialchars( $help_files[$help_files[0][$i]['file_id']][$c]['file_name']))),
							'item_contens'	=> nl2br( $appCore -> ifString( htmlspecialchars( $help_files[$help_files[0][$i]['file_id']][$c]['file_text'])))
						));
					}
					
					// Draw item
					$tpl_vars['help_files_list'] .= $templates -> tpl_contents_item( array(
						'item_id'		=> $help_files[0][$i]['file_id'],
						'item_name'		=> $appCore -> ifString( htmlspecialchars( $help_files[0][$i]['file_name'])),
						'item_contens'	=> $category_items
					));
					
					// Draw Body
					$tpl_vars['help_files'] .= $templates -> tpl_file_body( array(
						'item_id'				=> $help_files[0][$i]['file_id'],
						'item_name'				=> $appCore -> ifString( htmlspecialchars( $help_files[0][$i]['file_name'])),
						'item_description'		=> $appCore -> ifString( htmlspecialchars( $help_files[0][$i]['file_text'])),
						'item_contents'			=> $displayed_children
					));
				}
			}
		}
		
		// Draw output
		$appCore -> output -> draw( $appCore -> lang['help_system'], array(
			array( $appCore -> niceLink( 'act=help', $appCore -> lang['help_system']), $appCore -> lang['help_system'])
		), $templates -> tpl_page_body( $tpl_vars));
		
	}
	
}